﻿using SharpSoft.WPF.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace SharpSoft.WPF.Framework
{
    public class ListDataPage : DataPage
    {
        protected DataGrid datagrid;
        Grid head;
        protected BusyIndicator busy = new BusyIndicator() { Name = "busy" };
        public ListDataPage()
        {
            Grid grid = new Grid();
            DockPanel dock = new DockPanel() { LastChildFill = true };
            head = new Grid();
            datagrid = new DataGrid() { RowHeight = 22, CanUserAddRows = false, CanUserDeleteRows = false, SelectionUnit = DataGridSelectionUnit.FullRow, SelectionMode = DataGridSelectionMode.Single, IsReadOnly = true, AutoGenerateColumns = true };
            datagrid.SelectionChanged += Datagrid_SelectionChanged;
            datagrid.AutoGeneratingColumn += Datagrid_AutoGeneratingColumn;
            datagrid.MouseDoubleClick += Datagrid_MouseDoubleClick;

            dock.Children.Add(head);
            dock.Children.Add(datagrid);
            DockPanel.SetDock(head, Dock.Top);
            head.SetBinding(Grid.VisibilityProperty, new Binding("HeadVisibility") { Source = this });

            grid.Children.Add(dock);
            grid.Children.Add(busy);
            NameScope.SetNameScope(this, new NameScope());
            NameScope.GetNameScope(this).RegisterName("busy", busy);
            this.Content = grid;
            var headui = OnCreateHeadUI();
            if (headui != null)
            {
                head.Children.Add(headui);
            }
        }

        protected virtual UIElement OnCreateHeadUI()
        {
            return null;
        }


        public Visibility HeadVisibility
        {
            get { return (Visibility)GetValue(HeadVisibilityProperty); }
            set { SetValue(HeadVisibilityProperty, value); }
        }

        public static readonly DependencyProperty HeadVisibilityProperty =
            DependencyProperty.Register("HeadVisibility", typeof(Visibility), typeof(ListDataPage), new PropertyMetadata(Visibility.Collapsed));



        private void Datagrid_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                if (datagrid.SelectedItem != null)
                {
                    this.EditData();
                }
            }
        }

        /// <summary>
        /// 指定所有需要呈现的列
        /// </summary>
        public Dictionary<string, string> Headers { get; set; }
        private void Datagrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (Headers != null)
            {
                if (Headers.ContainsKey(e.PropertyName))
                {
                    e.Column.Header = Headers[e.PropertyName];
                }
                else
                {
                    e.Cancel = true;
                }

            }
        }

        private void Datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            OnSelectionChanged();
        }
        protected virtual void OnSelectionChanged()
        {
            if (datagrid.SelectedItem == null)
            {
                this.CanEdit = this.CanDelete = false;
            }
            else
            {
                this.CanEdit = this.CanDelete = true;
            }
        }
    }
}
